{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "israeli-porter",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "lovely-broadcasting",
   "metadata": {},
   "outputs": [],
   "source": [
    "#了解np\n",
    "dir(np)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "republican-divide",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 5, 7])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1. 根据序列创建1维数组\n",
    "a = np.array([1, 3, 5, 7])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "another-journalism",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#2. 根据序列创建二维数组\n",
    "a = np.array([(1,2,3),(4,5,6),(7,8,9)])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "portable-relative",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0., 0.],\n",
       "       [0., 1., 0., 0.],\n",
       "       [0., 0., 1., 0.],\n",
       "       [0., 0., 0., 1.]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3. 创建全0数组，其他同理：全1等\n",
    "a = np.zeros((3,4))\n",
    "a = np.ones((3,4))\n",
    "a = np.eye(4)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "optimum-closure",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 2, 1, 1, 2, 2, 2, 2, 1, 0, 2, 2, 1, 2, 2, 2, 2, 0, 1, 2, 1, 0,\n",
       "       2, 2, 0, 0, 2, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0,\n",
       "       2, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 2, 0, 1, 1, 2, 2, 1, 0, 2, 1, 0,\n",
       "       1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 2, 2, 0, 1, 1, 2, 1, 1, 0, 2,\n",
       "       0, 2, 2, 1, 0, 0, 1, 1, 1, 2, 0, 2])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4. 使用random模块创建\n",
    "a = np.random.randn(12).reshape((3,4))\n",
    "b = np.random.randint(0,3,size=100)\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "solved-marks",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 5. 使用arange\n",
    "a = np.arange(10).reshape((2,5))\n",
    "a "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "wound-survivor",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.  , 0.25, 0.5 , 0.75, 1.  , 1.25, 1.5 , 1.75, 2.  ])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 6. 线性\n",
    "a = np.linspace(0, 2, 9) \n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "flying-egypt",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 5)\n",
      "2\n",
      "int64\n",
      "15\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(15).reshape((3,5))\n",
    "print(a.shape)\n",
    "print(a.ndim)\n",
    "print(a.dtype)\n",
    "print(a.size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "closed-tackle",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "float64\n",
      "int32\n"
     ]
    }
   ],
   "source": [
    "# 默认数据类型是float64\n",
    "a = np.ones((3,4))\n",
    "\n",
    "# 可以指定数据类型\n",
    "b = np.ones((3,4), dtype='int32')\n",
    "print(a.dtype)\n",
    "print(b.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "beneficial-pottery",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
